
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

public class ClientGUI implements WindowListener {

	private JFrame dsFrame = null;  //  @jve:decl-index=0:visual-constraint="398,14"
	private JPanel dsWindow = null;

	private JMenuBar dsJMenuBar = null;
	private JMenuItem openMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private GraphicalView gv;
	private TreeView tv;
	private ButtonStrip bs;

	/**
	 * Constructor
	 */
	public ClientGUI(GraphicalView gv, TreeView tv, ButtonStrip bs) {
		this.gv = gv;
		this.tv = tv;
		this.bs = bs;

		try {
			getDsFrame();

		} catch(Exception ex) {
			System.out.println("ex: "+ex.getMessage());

		}
		System.out.println("Starting up the UI");
	}


	public void windowClosing(WindowEvent e) {

		try {
			this.gv.model.server.Detach(this.gv.model.getClientName());

			System.exit(0);

		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	/**
	 * This method initializes dsFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getDsFrame() {
		if (dsFrame == null) {
			dsFrame = new JFrame();
			dsFrame.setSize(new Dimension(1000, 600));
			dsFrame.setTitle("DrawSimply");
			dsFrame.setResizable(false);
			//dsFrame.setVisible(true);
			dsFrame.setJMenuBar(getDsJMenuBar());
			dsFrame.setContentPane(getDsWindow());

			dsFrame.addWindowListener(this);
			dsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			dsFrame.pack();
			dsFrame.setVisible(true);
		}

		System.out.println("Creating frame");
		return dsFrame;
	}

	/**
	 * This method initializes dsWindow	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getDsWindow() {
		if (dsWindow == null) {
			GridBagLayout griBagLayout = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();

			dsWindow = new JPanel();
			dsWindow.setName("");
			dsWindow.setLayout(griBagLayout);
			//c.
			c.gridx = 0;
			c.gridy = 0;
			c.fill = GridBagConstraints.VERTICAL;


			dsWindow.add(this.bs, c);

			c.gridx = 1;
			c.gridy = 0;
			//c.
			dsWindow.add(this.gv, c);


			c.gridx = 2;
			c.gridy = 0;
			dsWindow.add(new JScrollPane(this.tv), c);
			dsWindow.setVisible(true);


		}
		System.out.println("Creating window");
		return dsWindow;
	}


	private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {

		JFileChooser fileChooser = new javax.swing.JFileChooser();
		fileChooser.setDialogTitle("Open an existing draw...");
		int returnVal = fileChooser.showOpenDialog(openMenuItem);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			System.out.println(file.getName());
			Document doc = null;
			FileInputStream fin;
			try {
				fin = new FileInputStream(file.getAbsoluteFile());
				ObjectInputStream ois = new ObjectInputStream(fin);
			//	ois.readObject()
				doc = (Document) (ois.readObject());
				ois.close();


				this.gv.model.setDoc(doc);



			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}



		}
	}
	
	private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {

		JFileChooser fileChooser = new javax.swing.JFileChooser();
		fileChooser.setDialogTitle("Save a draw...");
		int returnVal = fileChooser.showSaveDialog(saveMenuItem);
		//if (returnVal == JFileChooser.SAVE_DIALOG) {
			File path = fileChooser.getCurrentDirectory();
			System.out.println("path: "+path.getAbsolutePath()+"\\"+fileChooser.getSelectedFile().getName());
			
			String files = path.getAbsolutePath()+"\\"+fileChooser.getSelectedFile().getName();
			
			
			
			FileOutputStream fout;
			try {
				fout = new FileOutputStream(files);
				 ObjectOutputStream oos = new ObjectOutputStream(fout);
			      oos.writeObject(this.gv.model.getDoc());
			      oos.close();
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		     
	}

	/**
	 * This method initializes dsJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getDsJMenuBar() {
		if (dsJMenuBar == null) {
			dsJMenuBar = new JMenuBar();


			String currentDir = System.getProperty("user.dir");
			currentDir+="\\resources";


			dsJMenuBar.setName("menuBar"); // NOI18N


			JMenu fileMenu = new JMenu();

			fileMenu.setText("File");
			fileMenu.setName("fileMenu"); // NOI18N


			JMenuItem newMenuItem = new JMenuItem();

			newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
			// newMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simpledraw/resources/New document.png"))); // NOI18N
			newMenuItem.setText("New");
			newMenuItem.setName("newMenuItem"); // NOI18N
			//	        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
			//	            public void actionPerformed(java.awt.event.ActionEvent evt) {
			//	                newMenuItemActionPerformed(evt);
			//	            }
			//	        });
			fileMenu.add(newMenuItem);


			openMenuItem = new JMenuItem();
			openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
			// openMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simpledraw/resources/Folder.png"))); // NOI18N
			openMenuItem.setText("Open");
			openMenuItem.setName("openMenuItem"); // NOI18N
			openMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					openMenuItemActionPerformed(evt);
				}
			});
			fileMenu.add(openMenuItem);

			JSeparator openSaveSeparator = new JSeparator();

			openSaveSeparator.setName("openSaveSeparator"); // NOI18N
			fileMenu.add(openSaveSeparator);

			saveMenuItem = new JMenuItem();

			saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
			saveMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\Save.png"));
			saveMenuItem.setText("Save");
			saveMenuItem.setName("saveMenuItem"); // NOI18N
			saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					saveMenuItemActionPerformed(evt);
				}
			});
			fileMenu.add(saveMenuItem);


			JMenu saveAsMenuItem = new JMenu();
			saveAsMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\Save.png"));
			saveAsMenuItem.setText("Save as...");
			saveAsMenuItem.setName("saveAsMenuItem"); // NOI18N


			JMenuItem pngMenuItem = new JMenuItem();
			pngMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\png.png"));
			pngMenuItem.setText("PNG Image");
			pngMenuItem.setName("pngMenuItem"); // NOI18N
			saveAsMenuItem.add(pngMenuItem);

			JMenuItem jpegMenuItem = new JMenuItem();
			jpegMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\jpeg.png"));
			jpegMenuItem.setText("JPEG Image");
			jpegMenuItem.setName("jpegMenuItem"); // NOI18N
			saveAsMenuItem.add(jpegMenuItem);

			fileMenu.add(saveAsMenuItem);


			JSeparator saveAsExitSeparator = new JSeparator();
			saveAsExitSeparator.setName("saveAsExitSeparator"); // NOI18N
			fileMenu.add(saveAsExitSeparator);

			//javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(simpledraw.SimpleDrawApp.class).getContext().getActionMap(SimpleDrawView.class, this);


			JMenuItem exitMenuItem = new JMenuItem();
			exitMenuItem.setName("exitMenuItem"); // NOI18N
			exitMenuItem.setText("GTFO");
			exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
			exitMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\Exit.png"));
			exitMenuItem.addActionListener(new ExitCommand(this.gv));
			fileMenu.add(exitMenuItem);

			dsJMenuBar.add(fileMenu);


			JMenu editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.setName("editMenu"); // NOI18N




			JMenuItem undoMenuItem = new JMenuItem();
			undoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
			undoMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\Undo.png")); // NOI18N
			undoMenuItem.setText("Undo");
			undoMenuItem.setName("undoMenuItem"); // NOI18N
			undoMenuItem.addActionListener(new UndoCommand(this.gv));
			editMenu.add(undoMenuItem);


			JMenuItem reDoMenuItem = new JMenuItem();
			reDoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
			reDoMenuItem.setIcon(new javax.swing.ImageIcon(currentDir+"\\Redo.png")); // NOI18N
			reDoMenuItem.setText("ReDo");
			reDoMenuItem.setName("reDoMenuItem"); // NOI18N
			reDoMenuItem.addActionListener(new RedoCommand(this.gv));
			editMenu.add(reDoMenuItem);







			dsJMenuBar.add(editMenu);





			//dsJMenuBar.add();


		}
		System.out.println("Creating menu bar");
		return dsJMenuBar;
	}


	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

}
